package com.xh.clean.modular.clean.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.xh.clean.core.base.controller.BaseController;
import com.xh.clean.core.cache.CacheKit;
import com.xh.clean.core.common.annotion.BussinessLog;
import com.xh.clean.core.common.constant.Const;
import com.xh.clean.core.common.constant.cache.Cache;
import com.xh.clean.core.common.constant.cache.CacheKey;
import com.xh.clean.core.common.constant.dictmap.DeleteDict;
import com.xh.clean.core.common.constant.dictmap.StoreyDict;
import com.xh.clean.core.common.constant.factory.ConstantFactory;
import com.xh.clean.core.log.LogObjectHolder;
import com.xh.clean.core.shiro.ShiroKit;
import com.xh.clean.core.support.StrKit;
import com.xh.clean.modular.clean.model.Storey;
import com.xh.clean.modular.clean.service.IStoreyService;
import com.xh.clean.modular.clean.warpper.StoreyWarpper;
import com.xh.clean.modular.system.service.IDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * cleanKeeping控制器
 *
 * @author yangjie
 * @Date 2018-06-23 16:26:45
 */
@Controller
@RequestMapping("/storey")
public class StoreyController extends BaseController {

    private String PREFIX = "/clean/storey/";

    @Autowired
    private IStoreyService storeyService;
    @Autowired
    private IDictService dictService;

    /**
     * 跳转到cleanKeeping首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "storey.html";
    }

    /**
     * 跳转到添加cleanKeeping
     */
    @RequestMapping("/storey_add")
    public String storeyAdd(Model model) {
        List<Map<String, Object>> patrolAreas = dictService.selectByPid(Const.PATROL_AREA_PID);
//        List<Map<String, Object>> patrolAreas = dictService.list("巡视区域");
        model.addAttribute("patrolAreas", patrolAreas);
        return PREFIX + "storey_add.html";
    }

    /**
     * 跳转到修改cleanKeeping
     */
    @RequestMapping("/storey_update/{storeyId}")
    public String storeyUpdate(@PathVariable Integer storeyId, Model model) {
        Storey storey = storeyService.selectById(storeyId);
        model.addAttribute("item", storey);
        String pareaTypes = storey.getPareaTypes();
        List<Map<String, Object>> patrolAreas = null;
        List<Map<String, Object>> selectPareas = new ArrayList<Map<String, Object>>();
        if (StrKit.isNotEmpty(pareaTypes)) {
            selectPareas = dictService.selectByInId(Arrays.asList(pareaTypes.split(StrKit.COMMA)));
            patrolAreas = dictService.selectByPidAndIdNotIn(Const.PATROL_AREA_PID, Arrays.asList(pareaTypes.split(StrKit.COMMA)));
        } else {
            patrolAreas = dictService.selectByPidAndIdNotIn(Const.PATROL_AREA_PID, null);
        }
        model.addAttribute("patrolAreas", patrolAreas);
        model.addAttribute("selectPareas", selectPareas);
        LogObjectHolder.me().set(storey);
        return PREFIX + "storey_edit.html";
    }

    /**
     * 获取卫生间操作列表
     */
    @RequestMapping(value = "/getPatrolAreas")
    @ResponseBody
    public Object getPatrolAreas(@RequestParam(required = false) Integer storeyId) {
        List<Map<String, Object>> selectPareas = new ArrayList<Map<String, Object>>();
        Wrapper<Storey> wrapper = new EntityWrapper<Storey>();
        if (storeyId != null) {
            wrapper.eq("id", storeyId);
        }
        List<Map<String, Object>> list = storeyService.selectMaps(wrapper);
        for (Map<String, Object> map : list) {
            Object pareaTypes = map.get("pareaTypes");
            if (pareaTypes != null) {
                String types = (String) pareaTypes;
                selectPareas = dictService.selectByInId(Arrays.asList(types.split(StrKit.COMMA)));
            }
        }
        return selectPareas;
    }

    /**
     * 获取cleanKeeping列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list = storeyService.selectStoreys(condition);
        return super.warpObject(new StoreyWarpper(list));
    }

    /**
     * 新增cleanKeeping
     */
    @RequestMapping(value = "/add")
    @BussinessLog(value = "添加办公楼", key = "name", dict = StoreyDict.class)
    @ResponseBody
    public Object add(Storey storey) {
        storey.setCreatetime(new Date());
        storey.setCreater(ShiroKit.getUser().getId());
        storeyService.insert(storey);
        return SUCCESS_TIP;
    }

    /**
     * 删除cleanKeeping
     */
    @RequestMapping(value = "/delete")
    @BussinessLog(value = "删除办公楼", key = "storeyId", dict = DeleteDict.class)
    @ResponseBody
    public Object delete(@RequestParam Integer storeyId) {
        LogObjectHolder.me().set(ConstantFactory.me().getStoreyNameById(storeyId));
        storeyService.deleteById(storeyId);
        CacheKit.remove(Cache.CONSTANT, CacheKey.STOREY_NAME + storeyId);
        return SUCCESS_TIP;
    }

    /**
     * 修改cleanKeeping
     */
    @RequestMapping(value = "/update")
    @BussinessLog(value = "修改办公楼", key = "id,name", dict = StoreyDict.class)
    @ResponseBody
    public Object update(Storey storey) {
        String pareaType = StrKit.removeSuffix(storey.getPareaTypes().toString(), StrKit.COMMA);
        storey.setPareaTypes(pareaType);
        CacheKit.remove(Cache.CONSTANT, CacheKey.STOREY_NAME + storey.getId());
        storeyService.updateById(storey);
        return SUCCESS_TIP;
    }

    /**
     * cleanKeeping详情
     */
    @RequestMapping(value = "/detail/{storeyId}")
    @ResponseBody
    public Object detail(@PathVariable("storeyId") Integer storeyId) {
        return storeyService.selectById(storeyId);
    }
}
